<?php


namespace app\api\model;


use app\common\model\BaseModel;
use common;
use ApiOper;
use hiredis;
use think\Db;

class RoleModel extends BaseModel
{
    protected $name = 'role';
    protected $pk = 'id';

    function role_level($role_id)
    {
        $arr = [
            STAFF_ROLE_SUPER_ADMIN => 0,
            STAFF_ROLE_CHAIRMAN => 10,
            STAFF_ROLE_CUSTOMER_SERVICE => 99,
        ];
        if (isset($arr[$role_id])) {
            return $arr[$role_id];
        }
        return STAFF_ROLE_LEVEL_RESERVED_MAX + 1;
    }

    function create_admin_role($city_id)
    {
        $rows = Db::name('acl_uri')->where('type','=','CC')->field('uri')->select();
        $uris = [];
        foreach ($rows as $row) {
            $uris[] = $row['uri'];
        }

        $info = [
            'city_id' => $city_id,
            'name' => '城市管理员',
            'remark' => '管理员',
            'type' => 'CC',
            'uris' => $uris,
            'state' => 0,
        ];

        return $this->add($info);
    }

    function ROLE_ID($city_id)
    {
        $sfx = '952700';
        if (!empty($city_id)) {
            $sfx = $city_id;
        }
        $key = 'ROLE_ID|' . $sfx;

        $id = hiredis::db()->incr($key);
        if ($id == 1) {
            $st = "select max(id) as maxid  from tb_role where id < 'a'";
            $beans = [];
            hisql::bound_arg('city_id', $city_id, $st, $beans);
            $result = hisql::db()->query($st, $beans);
            $rows = $result->fetchAll(Phalcon\Db::FETCH_ASSOC);

            if ($rows[0]['maxid'] === NULL) {
                $id = $sfx . "0001";
            } else {
                $id = strval(intval($rows[0]['maxid']) + 1);
            }
            hiredis::db()->set($key, $id);
        }
        return strval($id);
    }

    function add($info)
    {
        $paramlist = [
            'city_id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'state' => 'int',
            'type' => 'string',
            'level' => 'string',
        ];

        $data = common::type_conver($info, $paramlist);
        $data['id'] = $this->ROLE_ID($info['city_id']);

        ApiOper::instance()->append_creater_info($data);

        self::create($data,true);

        $this->set_role_uris($info['city_id'], $data['id'], $info['type'], $info['uri_ids']);

        return $data;
    }

    function query_list($city_id, $condition, $pos, $limit)
    {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'state' => 'int',
            'create_time' => 'string',
            'updater_id' => 'string',
            'update_time' => 'string',
            'updater_name' => 'string',
            'type' => 'string',
        ];

        $beans = [];

        if (isset($condition['is_base_role'])) {
            if ($condition['is_base_role']) {
                $beans['city_id'] = ['=',null];
            } else {
                $beans['city_id'] = ['=',$city_id];
            }
        } else {
            $beans['city_id'] = [['=',null],['=',$city_id],'or'];
        }

        if (!empty($condition['sk'])) {
            $beans['name'] = $condition['sk'];
        }

        if (!empty($condition['type'])) {
            $beans['type'] = $condition['type'];
        }
        if (isset($condition['level'])) {
            $beans['level'] = $condition['level'];
        }

        $rows = $this->where($beans)->field(common::array_keys($selectlist))->order('city_id')->limit($pos,$limit)->select();

        if ($rows->isEmpty()){
            return [];
        }
        return $rows->toArray();
    }

    function query_count($city_id, $condition)
    {
        $beans = [
            'city_id' => $city_id,
        ];

        if (!empty($condition['sk'])) {
            $beans['name'] = $condition['sk'];
        }

        return $this->where($beans)->count('id');
    }

    function query_info($city_id, $id)
    {
        $selectlist = [
            'city_id' => 'string',
            'id' => 'string',
            'name' => 'string',
            'remark' => 'string',
            'state' => 'string',
            'create_time' => 'string',
            'updater_id' => 'string',
            'update_time' => 'string',
            'updater_name' => 'string',
            'type' => 'string',
        ];
        $beans = [
            'id' => $id
        ];
        if (!empty($city_id)) {
            $beans['city_id'] = [['=',null],['=',$city_id],'or'];
        }

        $rows = $this->where($beans)->select();
        if (empty($rows)) {
            return null;
        }
        $info = common::sql_row($selectlist, $rows[0]);

        $uri_ids = (new ACLModel())->get_role_uri_ids($city_id, $info['id'], $info['type']);
        $info['uri_ids'] = $uri_ids;

        return $info;
    }

    function update_role($city_id, $id, $info)
    {
        $paramlist = [
            'name' => 'string',
            'remark' => 'string',
            'state' => 'int',
            'is_deleted' => 'int',
        ];

        $data = common::type_conver($info, $paramlist);

        ApiOper::instance()->append_updater_info($data);

        $where = [
            'city_id' => $city_id,
            'id' => $id,
        ];

        self::update($data, $where);


        $role_info = $this->query_info($city_id, $id);
        if (isset($info['uri_ids'])) {
            $this->set_role_uris($city_id, $id, $role_info['type'], $info['uri_ids']);
        }

        return $data;
    }

    function get_role_uris($city_id, $role_id)
    {
        $beans = [
            'city_id'=>$city_id,
            'role_id'=>$role_id,
            'is_deleted' => 0
        ];


        $rows = $this->where($beans)->field('uri_id')->select();
        if ($rows->isEmpty())
            return [];
        else
            return $rows->toArray();
    }

    function set_role_uris($city_id, $role_id, $type, $uri_ids)
    {
        $beans = [
            'city_id' => $city_id,
            'role_id' => $role_id,
            'type' => $type,
        ];

        self::where($beans)->destroy();

        $this->_role_add_uri($city_id, $role_id, $type, $uri_ids);
    }

    function _role_add_uri($city_id, $role_id, $type, $uri_ids)
    {
        $info = [
            'city_id' => $city_id,
            'role_id' => $role_id,
            'type' => $type,
        ];

        ApiOper::instance()->append_creater_info($info);

        foreach ($uri_ids as $uri_id) {
            $info['uri_id'] = $uri_id;
            self::create($info);
        }

        return $info;
    }
}